//此模块请求为普通请求,包括搜索,查询,查看别人主页详情等等,此模块请求不需要经过token认证,也就是不需要登录的操作.
const express = require('express')
const mysql = require('mysql')
const db = mysql.createPool({
    host: '127.0.0.1',
    user: 'root',
    password: '123456',
    database: 'paper'
})
let Router = express.Router()
Router.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', 'http://127.0.0.1:8080');
    res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization');
    res.set('Access-Control-Allow-Credentials', 'true');
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
    next();
})
Router.use(express.json())
Router.use(express.urlencoded({ extended: false }))

//根据用户id获取用户上传图片列表(自己或者他人的)
Router.get('/api/getuploadList', (req, res) => {
    let sql = `select * from uploadimg where userId = ?`
    db.query(sql, req.query.id, (err, result) => {
        if (err) {
            res.send({
                status: 500,
                msg: "err"
            })
        } else {
            res.send({
                status: 200,
                uploadList: result,
            })
        }
    })
})

//根据用户id获取用户收藏图片列表(自己或者他人的)
Router.get('/api/getCollections', (req, res) => {
    // console.log(req.query.userId)
    let userId = req.query.userId
    let sql = `select imgId,tags,imgPath,uploadId from collections where userId = ?`
    db.query(sql, userId, (err, result) => {
        res.send({
            status: 200,
            collections: result
        })
    })
})

//获取他人的头像以及主页背景
Router.get('/api/getOtherInfo', (req, res) => {
    let userId = req.query.userId
    let sql = `select userName,userImg,bgImg from user where id = ?`
    db.query(sql, userId, (err, result) => {
        res.send({
            status: 200,
            userName: result[0].userName,
            userImg: result[0].userImg,
            bgImg: result[0].bgImg
        })
    })
})

//首页图片加载
Router.get('/api/getHomeList', (req, res) => {
    let page = req.query.page
    //一次加载九张,page是第几页
    let sql = `select * from uploadimg limit ${(page - 1) * 9},9`
    db.query(sql, (err, result) => {
        res.send({
            status: 200,
            imgList: result
        })
    })
})


//搜索
Router.get('/api/serachImg', (req, res) => {
    let keyWork = req.query.keyWork
    keyWork = keyWork.split(',')
    let sql = `select * from uploadimg where tags like ?`
    let imgList = []
    //因为查询是异步的,这里需要等全部查询结果出来,并且把数组去重
    for (let i = 0; i < keyWork.length; i++) {
        db.query(sql, '%' + keyWork[i] + '%', (err, result) => {
            imgList = [...imgList, ...result]
            //当最后一次查询结束之后再返回结果
            if (i == keyWork.length - 1 && imgList.length != 0) {
                res.send({
                    status: 200,
                    List: [...new Set(imgList)]
                })
            } else if (i == keyWork.length - 1 && imgList.length == 0) {
                res.send({
                    status: 201,
                    msg: '抱歉,没有数据'
                })
            }
        })
    }
})


module.exports = Router